package com.jfeat.am.module.wegou.services.crud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jfeat.am.common.crud.FIELD;
import com.jfeat.am.common.crud.impl.CRUDServiceOverModelImpl;
import com.jfeat.am.common.exception.BizExceptionEnum;
import com.jfeat.am.common.exception.BusinessException;
import com.jfeat.am.module.wegou.services.crud.service.ProductStoreService;
import com.jfeat.am.module.wegou.services.domain.model.ProductStoreModel;
import com.jfeat.am.module.wegou.services.persistence.dao.ProductMapper;
import com.jfeat.am.module.wegou.services.persistence.dao.ProductStoreMapper;
import com.jfeat.am.module.wegou.services.persistence.dao.RestaurantTagMapper;
import com.jfeat.am.module.wegou.services.persistence.dao.TagManageMapper;
import com.jfeat.am.module.wegou.services.persistence.model.Product;
import com.jfeat.am.module.wegou.services.persistence.model.ProductStore;
import com.jfeat.am.module.wegou.services.persistence.model.RestaurantTag;
import com.jfeat.am.module.wegou.services.persistence.model.TagManage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 料理店铺 implementation
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-03
 */
@Service
public class ProductStoreServiceImpl extends CRUDServiceOverModelImpl<ProductStore, ProductStoreModel> implements ProductStoreService {


    @Resource
    private ProductStoreMapper productRestaurantMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    RestaurantTagMapper restaurantTagMapper;
    @Resource
    TagManageMapper tagMapper;

    private static final String RestaurantTagKeyName = "restaurantTags";
    private static final String RestaurantTagFieldName = RestaurantTag.STORE_ID;

    @Override
    protected String[] slaveFieldNames() {
        return new String[]{RestaurantTagKeyName};

    }

    @Override
    protected String[] childFieldNames() {
        return new String[0];
    }

    @Override
    protected FIELD onSlaveFieldItem(String s) {
        if (s.compareTo(RestaurantTagKeyName) == 0) {
            FIELD _field = new FIELD();
            _field.setItemKeyName(s);
            _field.setItemFieldName(RestaurantTagFieldName);
            _field.setItemClassName(RestaurantTag.class);
            _field.setItemMapper(restaurantTagMapper);
            return _field;
        }
        throw new BusinessException(BizExceptionEnum.OUT_OF_RANGE);
    }

    @Override
    protected FIELD onChildFieldItem(String s) {
        return null;
    }

    @Override
    protected Class<ProductStore> masterClassName() {
        return ProductStore.class;
    }

    @Override
    protected Class<ProductStoreModel> modelClassName() {
        return ProductStoreModel.class;
    }

    @Override
    protected BaseMapper<ProductStore> getMasterMapper() {
        // 单表 CRUD
        return productRestaurantMapper;
    }
    // Restaurant   Details   include Products  and  tags  -->  Model

    @Override
    public ProductStoreModel showRestaurant(long id) {
        // 料理店铺
        ProductStore restaurant = productRestaurantMapper.selectById(id);
        if (restaurant == null) {
            throw new BusinessException(4010, "无数据");
        }
        JSONObject resObj = JSON.parseObject(JSON.toJSONString(restaurant));
        //  插入 restaurant 的 product 属性
        List<Product> products = productMapper.selectList(new EntityWrapper<Product>().eq(Product.STORE_ID, restaurant.getId()));
        resObj.put("products", products);

        // 多对多中间表
        List<RestaurantTag> restaurantTags = restaurantTagMapper.selectList(new EntityWrapper<RestaurantTag>().eq(RestaurantTag.STORE_ID, restaurant.getId()));
        List<TagManage> tags = new ArrayList<>();
        if (restaurantTags.size() > 0) {
            for (RestaurantTag tag : restaurantTags) {
                TagManage tagManage = tagMapper.selectById(tag.getTagId());
                //防止TagManage被删除的情况
                if (tagManage != null) {
                    tags.add(tagManage);
                }
            }
        }
        // 标签名字
        resObj.put("tags", tags);
        resObj.put("restaurantTags", restaurantTags);
//        Map<String,Object> map = BeanKit.beanToMap(resObj);
        ProductStoreModel model = JSON.parseObject(JSON.toJSONString(resObj), ProductStoreModel.class);
        return model;
    }

    public ProductStore findFirstByUserId(Long userId) {
        ProductStore productRestaurant = new ProductStore();
        productRestaurant.setUserId(userId);
        return productRestaurantMapper.selectOne(productRestaurant);
    }


}


